var ysb={}; 

layui.use(['treeSelect','form', 'laydate','layer','layedit'], function(){
    var $ = layui.$
    ,element = layui.element
    ,layer = layui.layer
    ,laydate = layui.laydate
    ,form = layui.form;
    var token=localStorage.getToken;
    /**
     * 提交监听
     */
    form.on('submit(saveData)', function(data){
    	console.log(data.field);
    	var menuId=data.field.menuId;
    	var type=data.field.type;
    	var parentId=data.field.parentId;
    	var parentName=data.field.parentName;
    	var url=data.field.url;
    	var perms=data.field.perms;
    	var icon=data.field.icon;
    	var name=data.field.name;
    	var pathUrl="";
    	if(menuId==""||menuId==null){
    		pathUrl=baseApi+"/sys/menu/save";
    	}else{
    		pathUrl=baseApi+"/sys/menu/update";
    	}
        $.ajax({
        	type:"POST",
        	url:pathUrl,
        	data:{"name":name,"token":token,"menuId":menuId,"type":type,"parentId":parentId,"parentName":parentName,"url":url,"perms":perms,"icon":icon},
            success:function(data){
            	var code=data.code;
            	var msg=data.msg;
            	if(code=="0"||msg=="success"){
                     window.parent.refreshed();//刷新父页面
            		 layer.msg("保存成功");
            		 ysb.closeWind();
            	}else{
            		   layer.msg(data.msg);
            	}
            }
        });
    });
    
    //加载
    ysb.menuType=function(type){
	    $.ajax({
        	type:"GET",
        	url:baseApi+"/sys/dict/queryByCode",
        	data:{"token":token,"code":"MENU_TYPE"},
            success:function(data){
            	var code=data.code;
            	var msg=data.msg;
            	if(code=="0"&&msg=="success"){
            		 var menuTypeList=data.list;
            		 var strHtml="<select name=\"type\" id=\"type\" lay-verify=\"required\"><option value=\"\">直接选择或搜索选择</option>";
            		 for(var i=0;i<menuTypeList.length;i++){
            			 var selected="";
            			 if(type!=null){
            				 if(menuTypeList[i].value==type){
            					 selected="selected";
            				 }
            			 }
            			 strHtml+="<option "+selected+" value=\""+menuTypeList[i].value+"\">"+menuTypeList[i].name+"</option>"
            		 }
            		 strHtml+="</select>"
            		 $("#typeList").html(strHtml);
            		  form.render('select');
            	}else{
            	    layer.msg(data.msg);
            	}
            }
      }); 
   };
   
    
    /**
     * 关闭当前的窗口
     */
    ysb.closeWind=function(){
    	var index = parent.layer.getFrameIndex(window.name);
    	parent.layer.close(index);
    };
    //刷新父页面中的树形表格
    ysb.refreshed=function(){
    	var docId =parent.find("iframe")[0].contentWindow.document.getElementById("docId");
    }
    var treeSelect=layui.treeSelect;
    var index=layer.load(1); 
    treeSelect.render({
        elem: '#parentName',
        data: baseApi+'/sys/menu/select?token='+token,
        type: 'get',
        placeholder: '点击选择上级菜单',
        search: true,
        style: {
            folder: {
                enable: false
            },
            line: {
                enable: true
            }
        },
        click: function(d){
        	console.log(d);
        },
        // 加载完成后的回调函数
        success: function (d) {
            layer.close(index); 
            var menuId=localStorage.getMenuId;
            //如果是有ID进来，那就认为为修改
            if(menuId!=null&&menuId!=""){
         	   ysb.menuInfo(menuId);
            }else{
            	
            	ysb.menuType(null);
            }
              
        }
    });
    // 通过layer打开
    $('#layer').click(function(){
        layer.open({
            type: 1,
            title: 'Layer',
            content: $('.layui-container'),
            area: ['500px', '300px']
        });
    });
   //加载菜单详细信息
   ysb.menuInfo=function(menuId){
	    $.ajax({
        	type:"GET",
        	url:baseApi+"/sys/menu/info/"+menuId,
        	data:{"token":token},
            success:function(data){
            	var code=data.code;
            	var msg=data.msg;
            	if(code=="0"||msg=="success"){
            		var menu=data.menu;
            		$("#menuId").val(ysb.strNull(menu.menuId));
            		$("#parentId").val(ysb.strNull(menu.parentId));
            		$("#parentName").val(ysb.strNull(menu.parentName));
            		$("#name").val(ysb.strNull(menu.name));
            		$("#url").val(ysb.strNull(menu.url));
            		$("#perms").val(ysb.strNull(menu.perms));
            		$("#icon").val(ysb.strNull(menu.icon));
            		$("#orderNum").val(menu.orderNum);
            		$("#type").val(ysb.strNull(menu.type));
            		$("#type").find("option[value='"+menu.type+"']").attr("selected","selected");
            		$("#tmpParentName").val(ysb.strNull(menu.parentName));
            		ysb.menuType(menu.type);
            	}else{
            	    layer.msg(data.msg);
            	}
            	//清除加载数据
            	localStorage.getMenuId="";
            }
      }); 
   };

   //处理让自己讨厌的字符串（展示在页面上有伤大雅）
   ysb.strNull=function(str){
	   if(str==""||str=="null"||str==null){
		   return "";
	   }else{
		   return str;
	   }
   };
   
  });